<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class M_database extends BZ_Model
{
/**
 * @section Data Checking
 * @description Check the Data
 */

	/**
	 * @function total_rows
	 * @description get total record from a specified query
	 */
	function total_rows
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_table_where = ''
			, $prm_query_special = FALSE
			, $prm_query_or_like = ''
		)
	{
		$use_db = $this->{$prm_dbase};

		$use_db->from($prm_table_name);
		if (is_array($prm_table_where))
		{
			switch ($prm_query_special)
			{
				case TRUE:
					$use_db->where($prm_table_where, NULL, FALSE);
					break;

				default:
					$use_db->where($prm_table_where);
					break;
			}

		}
		else
		{
			if (trim($prm_table_where) != '')
			{
				$use_db->where($prm_table_where);
			}
		}

		/**
		 * @description need the or_like clause?
		 */
		if (is_array($prm_query_or_like))
		{
			$use_db->or_like($prm_query_or_like);
		}


		return $use_db->count_all_results();
	}


	/**
	 * @function last_insert_id
	 * @description get the last inserted ID
	 */
	function last_insert_id()
	{
		/**
		 * TODO: this should replace the max_id() to get the latest inserted ID; look for select max() vs select last insert id in google!
		 */
	}


	/**
	 * @function max_id
	 * @description get the maximum ID from the autonumber field
	 */
	function max_id
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_table_fields = '', $prm_table_where = ''
			, $prm_query_special = FALSE
		)
	{
		$use_db = $this->{$prm_dbase};

		$use_db->select_max($prm_table_fields, 'max_id');
		if (is_array($prm_table_where))
		{
			switch ($prm_query_special)
			{
				case TRUE:
					$use_db->where($prm_table_where, NULL, FALSE);
					break;

				default:
					$use_db->where($prm_table_where);
					break;
			}
		}
		else
		{
			if (trim($prm_table_where) != '')
			{
				$use_db->where($prm_table_where);
			}
		}


		$query = $use_db->get($prm_table_name);
		$return_value = $query->row()->max_id;
		$query->free_result();


		return $return_value;
	}


	/**
	 * @function datas
	 * @description get all the data from table
	 */
	function datas
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_table_where = '', $prm_table_fields = '*'
			, $prm_ajax_use = FALSE
			, $prm_fields_order = ''
			, $prm_query_or_like = ''
			, $prm_records_limit = 20, $prm_records_limit_offset = 0
			, $prm_query_special = FALSE
			, $prm_redirect_use = TRUE, $prm_redirect_url = ''
			, $prm_combo_use = FALSE
			, $prm_message_no_record = 'data_no_record'
		)
	{
		$use_db = $this->{$prm_dbase};

		/**
		 * @description select fields from table
		 */
		$use_db->select($prm_table_fields, FALSE);

		/**
		 * @description need the join clause?
		 */
		if (is_array($prm_table_name))
		{
			/**
			 * @description $prm_table_name as array? then proceed to use JOIN.
			 * 								with index array are: $prm_table_name['SOURCE'], $prm_table_name['JOIN'][]['TABLE'], $prm_table_name['JOIN'][]['KEYS'], $prm_table_name['JOIN'][]['TYPE']
			 */
			$use_db->from($prm_table_name['SOURCE']);
			foreach ($prm_table_name['JOIN'] as $table_join)
			{
				$use_db->join($table_join['TABLE'], $table_join['KEYS'], $table_join['TYPE']);
			}
		}

		/**
		 * @description need the where clause?
		 */
		if (is_array($prm_table_where) OR trim($prm_table_where) != '')
		{
			$use_db->where
				(
					$prm_table_where
					, NULL
					, ($prm_query_special == TRUE) ? FALSE : NULL
				);
		}

		/**
		 * @description need the or_like clause?
		 */
		if (is_array($prm_query_or_like))
		{
			$use_db->or_like($prm_query_or_like);
		}

		/**
		 * @description need the limit clause ?
		 */
		if (trim($prm_records_limit) != '')
		{
			/**
			 * @description init the pagination
			 */
			if ($prm_records_limit_offset == -1)
			{
				$prm_page_current = $this->pagination->cur_page - 1;
				if ($prm_page_current < 0) $prm_page_current = 0;

				$prm_records_limit_offset = ($prm_page_current * $prm_records_limit);
			}

			$use_db->limit($prm_records_limit, $prm_records_limit_offset);
		}

		/**
		 * @description need the order clause?
		 */
		if (trim($prm_fields_order) != '')
		{
			$use_db->order_by($prm_fields_order);
		}


		is_array($prm_table_name)
			? $query = $use_db->get()
			: $query = $use_db->get($prm_table_name);

		if ($query->num_rows() > 0)
		{
			switch ($prm_ajax_use)
			{
				case TRUE:
					/**
					 * @description return data for ajax purpose
					 */
					$fields = $query->field_data();
					foreach ($query->result_array() as $rows)
					{
						foreach ($fields as $field)
						{
							$return_value[] = $rows[$field->name];
						}
					}
					break;

				case FALSE:
					switch ($prm_combo_use)
					{
						case TRUE:
							foreach ($query->result() as $rows)
							{
								$return_value[$rows->combo_key] = $rows->combo_value;
							}
							break;

						default:
							$return_value = $query->result_array();
							break;
					}
					break;
			}


			return $return_value;

		}
		else
		{
			switch ($prm_redirect_use)
			{
				case TRUE:
					$return_value = 'location.replace("'.site_url($prm_redirect_url).'");';
					if ($prm_ajax_use == FALSE)
					{
						$return_value = '<script>'.$return_value.'</script>';
					}

					die($return_value);
					break;

				default:
					switch ($prm_combo_use)
					{
						case TRUE:
							return array();
							break;

						case FALSE:
							return $this->lang->line($prm_message_no_record);
							break;
					}
					break;
			}
		}


		$query->free_result();
	}



/**
 * @section DML
 * #description Simple Database Manipulation Language
 */

	/**
	 * @function insert
	 * @description insert data into database
	 */
	function insert
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_table_data = array()
			, $prm_fields_auto_use = TRUE, $prm_fields_autoincrement_use = TRUE
			, $prm_object_start = 0
			, $prm_message_success = 'data_insert_success', $prm_message_error = 'data_insert_error'
		)
	{
		$use_db = $this->{$prm_dbase};

		switch ($prm_fields_auto_use)
		{
			case TRUE:
				$fields = $use_db->list_fields($prm_table_name);
				$i = $prm_object_start;
				foreach ($fields as $field_name)
				{
					switch ($i)
					{
						case $prm_object_start:
							switch ($prm_fields_autoincrement_use)
							{
								case TRUE:
									$data[$field_name] = NULL;
									break;

								default:
									$data[$field_name] = $prm_table_data[$i];
									break;
							}
							break;

						default:
							$data[$field_name] = $prm_table_data[$i];
							break;
					}
					$i++;
				}
				break;

			default:
				$data = $prm_table_data;
				break;
		}


		$return_value = $use_db->insert($prm_table_name, $data);
		if ($return_value === TRUE)
		{
			$return_value = $this->lang->line($prm_message_success);
		}
		else
		{
			$return_value = $this->lang->line($prm_message_error);
		}


		return $return_value;
	}


	/**
	 * @function update
	 * @description update data in database
	 */
	function update
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_table_data = array(), $prm_table_where = array()
			, $prm_fields_auto_use = TRUE, $prm_object_start = 0, $prm_batch_use = FALSE
			, $prm_message_success = 'data_update_success', $prm_message_error = 'data_update_error'
		)
	{
		$data_is_exists = $this->total_rows($prm_dbase, $prm_table_name, $prm_table_where);
		if ($data_is_exists > 0)
		{
			$use_db = $this->{$prm_dbase};

			switch ($prm_fields_auto_use)
			{
				case TRUE:
					$fields = $use_db->list_fields($prm_table_name);
					$i = $prm_object_start;
					foreach ($fields as $field_name)
					{
						$data[$field_name] = $prm_table_data[$i];
						$i++;
					}
					break;

				case FALSE:
					$data = $prm_table_data;
					break;
			}

			if ($prm_batch_use == TRUE)
			{
				$use_db->update_batch($prm_table_name, $data, $prm_table_where);
			}
			else
			{
				$use_db->update($prm_table_name, $data, $prm_table_where);
			}

			$return_value = $this->lang->line($prm_message_success);

		}
		else
		{
			$return_value = $this->lang->line($prm_message_error);
		}


		return $return_value;
	}


	/**
	 * @function delete
	 * @description delete data in database
	 */
	function delete
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = '', $prm_table_where = array()
			, $prm_message_success = 'data_delete_success', $prm_message_error = 'data_delete_error'
		)
	{
		$data_is_exists = $this->total_rows($prm_dbase, $prm_table_name, $prm_table_where);
		if ($data_is_exists > 0)
		{
			$use_db = $this->{$prm_dbase};

			$return_value = $use_db->delete($prm_table_name, $prm_table_where);
			if ($return_value === FALSE)
			{
				$return_value = $use_db->_error_number();
			}
			else
			{
				$return_value = $this->lang->line($prm_message_success);
			}
		}
		else
		{
			$return_value = $this->lang->line($prm_message_error);
		}


		return $return_value;
	}


	/**
	 * @function delete_recursive
	 * @description delete the recursive data. if there is any sub data, then make it as the root
	 * WILL BE DEPRECATED!!!!!
	 */
	function delete_recursive
		(
			$prm_dbase = 'db_default'
			, $prm_table_name = ''
			, $prm_table_update_data = array(), $prm_table_update_where = array()
			, $prm_table_delete_where = array()
		)
	{
		$use_db = $this->{$prm_dbase};

		$use_db->select('count(1) record_count')->from($prm_table_name)->where($prm_table_where);
		$query = $use_db->get();

		if ($query->row()->record_count > 0)
		{
			$this->update
				(
					$prm_dbase
					, $prm_table_name
					, $prm_table_update_data
					, $prm_table_update_where
					, FALSE
				);
		}

		$return_value = $this->delete($prm_dbase, $prm_table_name, $prm_table_delete_where);


		return $return_value;
	}



}

/** End of file m_database.php */
/** Location: ./biskitz_mpc/biskitz/models/m_database.php */
